﻿$(function () {
    products.ProductUI();
    products.ProductMappingCategory();
    products.ProductSpecificationAttribute();
    products.ProductVariantAttributes();
    products.RelatedProduct();
});
var token = $('input[name="__RequestVerificationToken"]').val();
var productId = $('#product-id').val();
var productvariantId = $('#productvariant-id').val();

var products = {
    ProductUI: function () {
        $('#product-mapping-category').html("<img src='/Modules/Dyn.Ecommerce/Styles/images/loading.gif'/>")
                            .load("/Ecommerce/ProductAdmin/LoadCategoryProductMapping?prodId=" + productId);
    },
    ProductMappingCategory: function () {
        //Product Category Mapping
        $('#product-category-mapping input[type="text"]').live("blur", function () {
            var _id = $(this).attr("id");
            $('#text-hiden-' + _id + '').val($(this).val());
        });

        $('#product-category-mapping input[type="checkbox"]').live("click", function () {
            var _id = $(this).attr("id");
            if ($(this).is(':checked')) {
                $(this).next().show();
                $('#check-hiden-' + _id + '').val($(this).val());
            }
            else {
                $('#check-hiden-' + _id + '').val(0);
                $(this).next().hide();
            }
        });
    },
    ProductSpecificationAttribute: function () {
        //Product specification attribute 
        $('#specification-attribute-click').click(function () {
            var specification = $('#product-specification-attribute').html();
            if (specification == null) {
                $('.product-specification').html("<img src='/Modules/Dyn.Ecommerce/Styles/images/loading.gif'/>")
                        .load("/Ecommerce/ProductAdmin/LoadProductSpecificationAttribute?prodId=" + productId + "");
            }
        });
        $('#product-specification-attribute .attribute-name').live("change", function () {
            var _attribute = $(this).val();
            if (_attribute != "") {
                $('.attribute-option').load("/Ecommerce/ProductAdmin/LoadProductSpecificationAttributeOption?attributeId=" + _attribute + "");
            }
        });
        $('#product-specification-attribute #add-attribute').live("click", function () {
            var speci_attr = $('#product-specification-attribute .attribute-name').val();
            var option = $('#product-specification-attribute .attribute-option').val();
            var allow_filtering = $('#product-specification-attribute #allow-filtering').is(':checked');
            var show_product_page = $('#product-specification-attribute #Show-product-page').is(':checked');
            var display_order = $('#product-specification-attribute #display-order').val();
            if (option == null) {
                alert('Failed to add specification attribute.');
                return false;
            }
            $.post('/Ecommerce/ProductAdmin/AddContentProductSpecificationAttribute', { prodId: productId, specifiaction: speci_attr, option: option, filter: allow_filtering, display: display_order, show: show_product_page, __RequestVerificationToken: token },
                function (data) {
                    $('#product-specification-attribute tbody:last-child').append(data);
                }
            );
        });
        //Update specification attribute
        $('#product-specification-attribute a.edit').live("click", function () {
            var id = $(this).attr("href").replace('#', '');
            $("#label-spec-option-" + id + "").hide();
            $("#select-spec-option-" + id + "").show();
            $("#label-spec-filter-" + id + "").hide();
            $("#check-spec-filter-" + id + "").show();
            $("#label-spec-show-" + id + "").hide();
            $("#check-spec-show-" + id + "").show();
            $("#label-spec-display-" + id + "").hide();
            $("#text-spec-display-" + id + "").show();

            $("#update-spec-attr-" + id + "").show();
            $(this).hide();
        });
        $('#product-specification-attribute a.update').live("click", function () {
            var id = $(this).attr("href").replace('#', '');
            $("#label-spec-option-" + id + "").show();
            $("#select-spec-option-" + id + "").hide();
            $("#label-spec-filter-" + id + "").show();
            $("#check-spec-filter-" + id + "").hide();
            $("#label-spec-show-" + id + "").show();
            $("#check-spec-show-" + id + "").hide();
            $("#label-spec-display-" + id + "").show();
            $("#text-spec-display-" + id + "").hide();
            $("#edit-spec-attr-" + id + "").show();
            $(this).hide();
            var option = $("#product-specification-attribute #select-spec-option-" + id + "").val();
            var filter = $("#product-specification-attribute #check-spec-filter-" + id + "").is(':checked');
            var show = $("#product-specification-attribute #check-spec-show-" + id + "").is(':checked');
            var display = $("#product-specification-attribute #text-spec-display-" + id + "").val();
            var optionName = $("#product-specification-attribute #select-spec-option-" + id + " option:selected").text();
            $.post('/Ecommerce/ProductAdmin/UpdateContentProductSpecificationAttribute', { id: id, option: option, filter: filter, display: display, show: show, __RequestVerificationToken: token },
                      function (data) {
                          $("#label-spec-option-" + id + "").text(optionName);
                          $("#label-spec-filter-" + id + "").text(filter);
                          $("#label-spec-show-" + id + "").text(show);
                          $("#label-spec-display-" + id + "").text(display);
                      }
                    );
        });
        $('#product-specification-attribute a.delete').live("click", function () {
            var id = $(this).attr("href").replace('#', '');
            $.post('/Ecommerce/ProductAdmin/DeleteContentProductSpecificationAttribute', { id: id, __RequestVerificationToken: token },
                function (data) {
                    $('#product-specification-attribute tr#spec-row-' + id + '').remove();
                });
        });
    },
    //Product Variant Attribute 
    ProductVariantAttributes: function () {
        $('#variant-attribute-click').click(function () {

            var specification = $('#product-variant-attribute').html();
            if (specification == null) {
                $('.product-variant-atrributes').html("<img src='/Modules/Dyn.Ecommerce/Styles/images/loading.gif'/>")
                        .load("/Ecommerce/ProductAdmin/LoadProductVariantAttributes?variantId=" + productvariantId + "");
            }
        });
        $('#product-variant-attribute #add-variant-attribute').live("click", function () {
            var product_attribute = $('#product-variant-attribute #product-attribute').val();
            var text_prompt = $('#product-variant-attribute #text-prompt').val();
            var is_required = $('#product-variant-attribute #is-required').is(':checked');
            var control_type = $('#product-variant-attribute #control-type').val();
            var display_order = $('#product-variant-attribute #display-order').val();

            $.post('/Ecommerce/ProductAdmin/AddProductVariantAttribute', { variantId: productvariantId, productAttr: product_attribute, textPrompt: text_prompt, isRequired: is_required, display: display_order, controlType: control_type, __RequestVerificationToken: token },
                function (data) {
                    $('#product-variant-attribute tbody:last-child').append(data);
                }
            );
        });
        //Update specification attribute
        $('#product-variant-attribute a.edit').live("click", function () {
            var id = $(this).attr("href").replace('#', '');
            $("#label-var-textprompt-" + id + "").hide();
            $("#text-var-textprompt-" + id + "").show();
            $("#label-var-isrequired-" + id + "").hide();
            $("#check-var-isrequired-" + id + "").show();
            $("#label-var-display-" + id + "").hide();
            $("#text-var-display-" + id + "").show();
            $("#update-variant-attr-" + id + "").show();
            $(this).hide();
        });
        $('#product-variant-attribute a.update').live("click", function () {
            var id = $(this).attr("href").replace('#', '');
            $("#label-var-textprompt-" + id + "").show();
            $("#text-var-textprompt-" + id + "").hide();
            $("#label-var-isrequired-" + id + "").show();
            $("#check-var-isrequired-" + id + "").hide();
            $("#label-var-display-" + id + "").show();
            $("#text-var-display-" + id + "").hide();
            $("#edit-variant-attr-" + id + "").show();
            $(this).hide();
            var textprompt = $("#product-variant-attribute #text-var-textprompt-" + id + "").val();
            var isrequired = $("#product-variant-attribute #check-var-isrequired-" + id + "").is(':checked');
            var display = $("#product-variant-attribute #text-var-display-" + id + "").val();
            $.post('/Ecommerce/ProductAdmin/UpdateProductVariantAttribute', { id: id, textprompt: textprompt, isrequired: isrequired, display: display, __RequestVerificationToken: token },
                    function (data) {
                        $("#label-var-textprompt-" + id + "").text(textprompt);
                        $("#label-var-isrequired-" + id + "").text(isrequired);
                        $("#label-var-display-" + id + "").text(display);
                    }
                );
        });
        $('#product-variant-attribute a.delete').live("click", function () {
            var id = $(this).attr("href").replace('#', '');
            $.post('/Ecommerce/ProductAdmin/DeleteProductVariantAttribute', { id: id, __RequestVerificationToken: token },
            function (data) {
                $('#product-variant-attribute tr#variant-attribute-' + id + '').remove();
            });
        });
    },
    RelatedProduct: function () {
        $('#related-product-click').click(function () {
            var relatedProduct = $('#product-related').html();
            if (relatedProduct == null) {
                $('.related-product').html("<img src='/Modules/Dyn.Ecommerce/Styles/images/loading.gif'/>")
                    .load("/Ecommerce/ProductAdmin/LoadRelatedProduct?prodId=" + productId + "");
            }
        });
        $('#product-related a.edit').live("click", function () {
            var contentId = $(this).attr("href").replace('#', '');
            $("#product-related #text-display-" + contentId + "").show();
            $("#product-related #label-display-" + contentId + "").hide();
            $("#product-related #product-update-" + contentId + "").show();
            $(this).hide();
        });
        $('#product-related a.update').live("click", function () {
            var contentId = $(this).attr("href").replace('#', '');
            $("#product-related #product-edit-" + contentId + "").show();
            $("#product-related #text-display-" + contentId + "").hide();
            $("#product-related #label-display-" + contentId + "").show();
            $(this).hide();
            //Update content-product
            var displayIndex = $("#product-related #text-display-" + contentId + "").val();
            $.post('/Ecommerce/ProductAdmin/UpdateRelatedProduct', { displayIndex: displayIndex, id: contentId, __RequestVerificationToken: token },
                    function (data) {
                        $("#product-related #label-display-" + contentId + "").text(displayIndex);
                    }
                );
        });
        $('#product-related a.c_delete').live("click", function () {
            var contentId = $(this).attr("href").replace('#', '');
            $.post('/Ecommerce/ProductAdmin/DeleteRelatedProduct', { id: contentId, __RequestVerificationToken: token },
                    function (data) {
                        $('#product-related tr#product-row-' + contentId + '').remove();
                    }
                );
        });
        $('#add-related-product').live("click", function (event) {
            var url = $(this).attr("href");
            var windowName = $(this).attr("name");
            var windowSize = "width=700,height=800,scrollbars=yes";
            window.open(url, windowName, windowSize);
            event.preventDefault();
        });
    }
};


    